﻿#include "settingdialog.h"
#include "ui_settingdialog.h"

#include <QMediaPlayer>

SettingDialog::SettingDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingDialog)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

    ui->lineEdit_name->setText(ST->settings()->value("PlayerName", QDir::home().dirName()).toString());
    ui->switchButton_speedon->setChecked(ST->settings()->value("SpeedOn", true).toBool());
    ui->switchButton_showrecord->setChecked(ST->settings()->value("ShowRecord", true).toBool());
    ui->xslider_music->setValue(ST->settings()->value("MusicLevel", 3).toInt());
    ui->xslider_sound_effect->setValue(ST->settings()->value("SoundEffectLevel", 3).toInt());
    ui->switchButton_extendblocks->setChecked(ST->settings()->value("ExtendblocksOn", false).toBool());
    int mode = ST->settings()->value("GameMode", 1).toInt();
    switch (mode) {
    case StandardMode:
        ui->rbt_standard->setChecked(true);
        break;
    case PassMode:
        ui->rbt_pass->setChecked(true);
        break;
    default:
        break;
    }
}

SettingDialog::~SettingDialog()
{
    delete ui;
}

/**
 * @brief   重写accept函数，加入参数保存功能
 * @details
 */
void SettingDialog::accept()
{
    if (!ui->lineEdit_name->text().isEmpty()) {
        ST->settings()->setValue("PlayerName", ui->lineEdit_name->text());
    }
    ST->settings()->setValue("SpeedOn", ui->switchButton_speedon->getChecked());
    ST->settings()->setValue("ShowRecord", ui->switchButton_showrecord->getChecked());
    ST->settings()->setValue("MusicLevel", ui->xslider_music->value());
    ST->settings()->setValue("SoundEffectLevel", ui->xslider_sound_effect->value());
    ST->settings()->setValue("ExtendblocksOn", ui->switchButton_extendblocks->getChecked());
    if (ui->rbt_standard->isChecked()) {
        ST->settings()->setValue("GameMode", 0);
    } else if (ui->rbt_pass->isChecked()) {
        ST->settings()->setValue("GameMode", 1);
    }
    QDialog::accept();
}
